package com.gin.kotlin.no_gay_alliance.adapter.me

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity
import com.gin.kotlin.no_gay_alliance.activity.UserCenterActivity
import com.gin.kotlin.no_gay_alliance.activity.pm.PrivateMessageReplyActivity
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.argument.PrivateMessageReplyArgument
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.database.entity.NoticeMsg
import com.gin.kotlin.no_gay_alliance.databinding.ItemNoticeBinding
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils
import com.gin.nga.enums.NoticeType.COMMENT_REPLY
import com.gin.nga.enums.NoticeType.COMMENT_TOPIC
import com.gin.nga.enums.NoticeType.MENTION_REPLY
import com.gin.nga.enums.NoticeType.MENTION_TOPIC
import com.gin.nga.enums.NoticeType.MESSAGE_ADD
import com.gin.nga.enums.NoticeType.MESSAGE_NEW
import com.gin.nga.enums.NoticeType.MESSAGE_REPLY
import com.gin.nga.enums.NoticeType.RECOMMEND_POST
import com.gin.nga.enums.NoticeType.REPLY_REPLY
import com.gin.nga.enums.NoticeType.REPLY_TOPIC
import com.gin.nga.enums.NoticeType.UPVOTE

class NoticeAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemNoticeBinding, NoticeMsg>(context, handler), View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemNoticeBinding>, position: Int, b: ItemNoticeBinding, dataModel: NoticeMsg) {
        b.tvTag.text = dataModel.type?.tag
        b.tvUnreadFlag.visibility = if (dataModel.unread) View.VISIBLE else View.INVISIBLE
        b.tvNoticeTimestamp.text = TimeUtils.SIMPLE_DATE_TIME_FORMATTER.format(dataModel.time)

        b.tvNoticeDescription.text = SpannableStringBuilder().also { builder ->
            builder.append(dataModel.description)
            dataModel.description?.also { dsp ->
                //染色: 主题标题 用户名
                listOfNotNull(dataModel.text, dataModel.fromUname).forEach { text ->
                    val index = dsp.indexOf(text)
                    if (index >= 0) {
                        val span = ForegroundColorSpan(getAttrColor(R.attr.textColorBlue))
                        builder.setSpan(span, index, index + text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                }
            }

        }

        b.ivReply.visibility = if (dataModel.aboutId3 != null && dataModel.type != COMMENT_REPLY) View.VISIBLE else View.GONE
        b.ivTopic.visibility = if (dataModel.aboutId != null && dataModel.aboutId!! > 0 && dataModel.type?.tag != "私信") View.VISIBLE else View.GONE
        b.ivUserCenter.visibility = if (dataModel.fromUid != null && dataModel.fromUid!! > 0) View.VISIBLE else View.GONE


        listOf(b.tvNoticeDescription, b.tvNoticeTimestamp, b.tvUnreadFlag, b.ivReply, b.ivTopic, b.ivUserCenter).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        val noticeMsg = v?.tag as? NoticeMsg ?: return

        when (v.id) {
            R.id.iv_reply -> openReply(noticeMsg.aboutId3!!)
            R.id.iv_topic -> openTopic(noticeMsg.aboutId!!)
            R.id.iv_user_center -> UserCenterActivity.startActivity(context, noticeMsg.fromUid!!)
            else -> when (noticeMsg.type) {
                //  回复你的主题、回复 或 回复中提到你： 跳转到该回复 SINGLE
                REPLY_TOPIC, REPLY_REPLY, MENTION_REPLY -> openReply(noticeMsg.aboutId2!!)
                //  评论你的主题 或 主题中提到你：跳转到主题
                COMMENT_TOPIC, MENTION_TOPIC -> openTopic(noticeMsg.aboutId!!)
                //  评论你的回复：跳转到你的回复
                COMMENT_REPLY -> openReply(noticeMsg.aboutId3!!)
                // 送礼 或 赞数更新，跳转到你的主题或回复
                RECOMMEND_POST, UPVOTE -> if (noticeMsg.aboutId2 != null) openReply(noticeMsg.aboutId2!!) else openTopic(noticeMsg.aboutId!!)
                //  私信消息，跳转到该私信
                MESSAGE_NEW, MESSAGE_REPLY, MESSAGE_ADD -> PrivateMessageReplyActivity.start(
                    context,
                    PrivateMessageReplyArgument(noticeMsg.aboutId!!, fromSystem = noticeMsg.fromUid == null || noticeMsg.fromUid == 0L)
                )

                else -> handler.toast("未开发功能")
            }
        }
        read(noticeMsg)
    }

    /**
     * 设置已读
     * @param noticeMsg NoticeMsg
     */
    private fun read(noticeMsg: NoticeMsg) {
        // 清除未读标记
        App.INSTANCE.database.noticeMsgDao().read(noticeMsg.uuid)
        noticeMsg.unread = false
        notifyItemChanged(data.indexOf(noticeMsg))
    }

    private fun openReply(replyId: Long) = ReplyListActivity.startActivity(context, ReplyListArgument(type = ReplyListArgument.Type.SINGLE, replyId = replyId))
    private fun openTopic(topicId: Long) = ReplyListActivity.startActivity(context, ReplyListArgument(type = ReplyListArgument.Type.LIST, topicId = topicId))

}